home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / mdel.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  4KB  |  164 lines

  1. /*
  2.  * Delete an MSDOS file
  3.  *
  4.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  5.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  6.  * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  7.  *                     Environmental Management Office
  8.  *                     Fort Hood, TX 76544-5057
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <signal.h>
  13. #include "msdos.h"
  14. #include "patchlevel.h"
  15.  
  16. int fd = -1;                /* the file descriptor for the device */
  17. int dir_start;                /* starting sector for directory */
  18. int dir_len;                /* length of directory (in sectors) */
  19. int dir_entries;            /* number of directory entries */
  20. int clus_size;                /* cluster size (in sectors) */
  21. char *mcwd;                /* the Current Working Directory */
  22. int fat_error;                /* FAT error detected? */
  23.  
  24. static int got_signal();
  25.  
  26. main(argc, argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.     int i, ismatch, entry, nogo, verbose, fargn, got_one, missed_one;
  31.     unsigned int start;
  32.     char *filename, *newfile, *get_name(), *unix_name(), *get_path();
  33.     char *pathname, ans[10], drive, get_drive(), last_drive, *fix_mcwd();
  34.     void exit(), fat_write(), dir_write(), dir_flush(), disk_flush();
  35.     struct directory *dir, *dir_read();
  36.                     /* catch signals */
  37.     signal(SIGINT, (SIG_TYPE(*) ()) got_signal);
  38.     signal(SIGTERM, (SIG_TYPE(*) ()) got_signal);
  39.     signal(SIGQUIT, (SIG_TYPE(*) ()) got_signal);
  40.  
  41.     got_one = 0;
  42.     missed_one = 0;
  43.  
  44.     if (argc > 1 && !strcmp(argv[1], "-v")) {
  45.         verbose = 1;
  46.         fargn = 2;
  47.     }
  48.     else {
  49.         verbose = 0;
  50.         fargn = 1;
  51.     }
  52.     if (argc < 2 || (argv[1][0] == '-' && !verbose)) {
  53.         fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  54.         fprintf(stderr, "Usage: %s [-v] msdosfile [msdosfiles...]\n", argv[0]);
  55.         exit(1);
  56.     }
  57.     last_drive = 'x';
  58.     mcwd = fix_mcwd();
  59.  
  60.     for (i = fargn; i < argc; i++) {
  61.         drive = get_drive(argv[i]);
  62.         if (drive != last_drive) {
  63.             if (last_drive != 'x') {
  64.                 fat_write();
  65.                 dir_flush();
  66.                 disk_flush();
  67.             }
  68.  
  69.             if (init(drive, 2)) {
  70.                 fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  71.                 missed_one++;
  72.                 continue;
  73.             }
  74.             last_drive = drive;
  75.         }
  76.         filename = get_name(argv[i]);
  77.         pathname = get_path(argv[i]);
  78.         if (subdir(drive, pathname)) {
  79.             missed_one++;
  80.             continue;
  81.         }
  82.  
  83.         nogo = 0;
  84.         ismatch = 0;
  85.         for (entry = 0; entry < dir_entries; entry++) {
  86.             dir = dir_read(entry);
  87.                     /* if empty */
  88.             if (dir->name[0] == 0x0)
  89.                 break;
  90.                     /* if erased */
  91.             if (dir->name[0] == 0xe5)
  92.                 continue;
  93.                     /* if dir or volume label */
  94.             if ((dir->attr & 0x10) || (dir->attr & 0x08))
  95.                 continue;
  96.  
  97.             newfile = unix_name(dir->name, dir->ext);
  98.                     /* see it if matches the pattern */
  99.             if (match(newfile, filename)) {
  100.                 if (verbose)
  101.                     printf("Removing %s\n", newfile);
  102.                 ismatch = 1;
  103.                 if (dir->attr & 0x01) {
  104.                     while (!nogo) {
  105.                         printf("%s: \"%s\" is read only, erase anyway (y/n) ? ", argv[0], newfile);
  106.                         gets(ans);
  107.                         if (ans[0] == 'y' || ans[0] == 'Y')
  108.                             break;
  109.                         if (ans[0] == 'n' || ans[0] == 'N')
  110.                             nogo = 1;
  111.                     }
  112.                     if (nogo)
  113.                         continue;
  114.                 }
  115.                 start = dir->start[1] * 0x100 + dir->start[0];
  116.                 if (fat_free(start))
  117.                     break;
  118.                 dir->name[0] = 0xe5;
  119.                 dir_write(entry, dir);
  120.                 got_one++;
  121.             }
  122.         }
  123.         if (fat_error) {
  124.             missed_one++;
  125.             break;
  126.         }
  127.  
  128.         if (!ismatch) {
  129.             fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  130.             missed_one++;
  131.         }
  132.     }
  133.                     /* write the FAT, flush the buffers */
  134.     fat_write();
  135.     dir_flush();
  136.     disk_flush();
  137.     close(fd);
  138.     if (got_one && missed_one)
  139.         exit(2);
  140.     if (missed_one)
  141.         exit(1);
  142.     exit(0);
  143. }
  144.  
  145. /*
  146.  * Do a graceful exit if the program is interrupted.  This will reduce
  147.  * (but not eliminate) the risk of generating a corrupted disk on
  148.  * a user abort.
  149.  */
  150.  
  151. static int
  152. got_signal()
  153. {
  154.     void exit(), disk_flush(), fat_write(), dir_flush();
  155.  
  156.     if (fd < 0)
  157.         exit(1);
  158.     fat_write();
  159.     dir_flush();
  160.     disk_flush();
  161.     close(fd);
  162.     exit(1);
  163. }
  164.